package com.atlin.beaconsapiinterface.controller;


import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import com.atlin.beaconsapiclientsdk.util.SignUtil;
import com.atlin.beaconsapiinterface.model.request.WeatherRequest;
import com.atlin.beaconsapiinterface.model.response.WeatherResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.Random;

/**
 * @author atlin
 * @description
 * @createDate 2024/12/7 12:58
 */
@Slf4j
@RestController
@RequestMapping("/weather")
public class WeatherController {

    @GetMapping("/getWeatherText")
    public String getWeatherText(HttpServletRequest request) {
//        signCheck(request);
        log.info("invoke getWeatherText");
        return weather();
    }

    @GetMapping("/getWeatherGet")
    public WeatherResponse getWeatherGet(String params, HttpServletRequest request) {
//        signCheck(request);
        WeatherResponse weatherResponse = new WeatherResponse();
        weatherResponse.setValue(weather() + " GET").setCity("长安");

        log.info("invoke getWeatherGet");
        return weatherResponse;
    }

    @GetMapping("/getWeatherUserText")
    public WeatherResponse getWeatherUserText(String name, Integer age, HttpServletRequest request) {
//        signCheck(request);
        WeatherResponse weatherResponse = new WeatherResponse();
        weatherResponse.setValue(weather() + " GET" + name + age).setCity("长安");

        log.info("invoke getWeatherUserText");
        return weatherResponse;
    }

    @PostMapping("/getWeather")
    public String getWeather(@RequestParam String params, HttpServletRequest request) {
//        signCheck(request);
        log.info("invoke getWeather");
        return weather() + " POST " + params;
    }

    @PostMapping("/getWeatherObj")
    public WeatherResponse getWeatherObj(@RequestBody WeatherRequest weatherRequest, HttpServletRequest request) {
        signCheck(request);
        String city = weatherRequest.getCity();
        String ip = weatherRequest.getIp();
        String type = weatherRequest.getType();

        WeatherResponse weatherResponse = new WeatherResponse();
        weatherResponse.setValue(weather() + " -- " + type + " -- " + ip).setCity(city);
        log.info("invoke getWeatherObj");
        return weatherResponse;
    }

    private String weather() {
        return (new Random().nextInt(2)) == 0 ? "晴" : "阴";
    }

    private void signCheck(HttpServletRequest request) {
        String accessKey = request.getHeader("accessKey");
        String timestamp = request.getHeader("timestamp");
        String once = request.getHeader("once");
        String dataBody = request.getHeader("dataBody");
        String body = request.getHeader("body");

        String sign = request.getHeader("sign");

        // 根据accessKey查询数据库 得到 secretKey  
        if (!"atlin".equals(accessKey)) {
            throw new RuntimeException("无权限");
        }
        String secretKey = "abcdef";
        final long FIVE_M = 5 * 60;
        if ((System.currentTimeMillis() / 1000) - Long.parseLong(timestamp) > FIVE_M) {
            throw new RuntimeException("无权限");
        }
//        if (Long.parseLong(once) > 10000) {
//            throw new RuntimeException("无权限");
//        }

        String serviceSign = null;
        try {
            serviceSign = SignUtil.genSign(body, secretKey);
        } catch (Exception e) {
            throw new RuntimeException("Fail decode dataBody.");
        }

        if (!serviceSign.equals(sign)) {
            throw new RuntimeException("无权限");
        }

    }
}
